import {
    defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'

import path from "path"

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [vue(), WindiCSS()],
    resolve: {
        alias: {
            "~": path.resolve(__dirname, "src"),
        }
    },
    server: {
        host: "0.0.0.0",
        port: 8088,
        proxy: {
            // 带选项写法：http://localhost:5173/api/bar -> http://jsonplaceholder.typicode.com/bar
            '/api': {
                target: 'http://127.0.0.1:8000/',
                // target: 'https://api.xjwht.cn/',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
            },
            '/media/image/user/': {
                target: 'http://127.0.0.1:8000/',
                // target: 'https://api.xjwht.cn/media/',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
            },
            '/image/user/': {
              target: 'http://127.0.0.1:8000/media/',
              // target: 'https://api.xjwht.cn/media/',
              changeOrigin: true,
              rewrite: (path) => path.replace(/^\/api/, ''),
          },

        },
    },build: {
        chunkSizeWarningLimit: 1500, // 调整包的大小
        rollupOptions: {
          output: {
            // 最小化拆分包
            manualChunks(id) {
              if (id.includes('node_modules')) {
                return id.toString().split('node_modules/')[1].split('/')[0].toString()
              }
            },
            // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
            entryFileNames: 'assets/js/[name].[hash].js', // 用于命名代码拆分时创建的共享块的输出命名
            chunkFileNames: 'assets/js/[name].[hash].js', // 用于输出静态资源的命名，[ext]表示文件扩展名
            assetFileNames: 'assets/[ext]/[name].[hash].[ext]'
          }
        }
      },

})